package tx.yt.charge.service;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tx.yt.charge.model.Distribution;
import tx.yt.charge.utils.PageUtil;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 分拨点表业务
 * Created by ZJ on 2015/1/14.
 */
@Service
public class DistributionService {
    @Resource
    private JdbcTemplate jdbcTemplate;

    /**
     * 根据分拨点编号查询分拨信息
     *
     * @param id
     * @return
     */
    public Distribution queryDistribution(int id) {
        final Distribution distribution = new Distribution();
        String sql = "select id,distributionName from distribution where id = ?";
        this.jdbcTemplate.query(sql, new Object[]{id}, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet resultSet) throws SQLException {
                distribution.setId(resultSet.getInt("id"));
                distribution.setDistributionName(resultSet.getString("distributionName"));
            }
        });
        return distribution;
    }

    /**
     * 根据分拨点信息查询分拨点列表
     *
     * @param distribution
     * @return
     */
    public List<Distribution> queryDistributions(Distribution distribution) {
        StringBuilder sql = new StringBuilder("select id,distributionName from distribution where 1 = 1");
        if (0 != distribution.getId()) {
            sql.append(" and id = " + distribution.getId());
        }
        if (!StringUtils.isEmpty(distribution.getDistributionName())) {
            sql.append(" and distributionName = '" + distribution.getDistributionName() + "'");
        }

        List<Distribution> result = new ArrayList<Distribution>();
        Distribution dis = null;
        List lists = this.jdbcTemplate.queryForList(sql.toString());
        Iterator it = lists.iterator();
        while (it.hasNext()) {
            Map map = (Map) it.next();
            dis = new Distribution();
            dis.setId(Integer.valueOf(String.valueOf(map.get("id"))));
            dis.setDistributionName((String) map.get("distributionName"));
            result.add(dis);
        }

        return result;
    }

    /**
     * 根据分拨点信息查询分拨点列表(分页)
     *
     * @param distribution
     * @param currentPage
     * @return
     */
    public List<Distribution> queryDistributions(Distribution distribution, int currentPage) {
        StringBuilder sql = new StringBuilder("select id,distributionName from distribution where 1 = 1");
        if (0 != distribution.getId()) {
            sql.append(" and id = " + distribution.getId());
        }
        if (!StringUtils.isEmpty(distribution.getDistributionName())) {
            sql.append(" and distributionName = '" + distribution.getDistributionName() + "'");
        }
        sql.append(" order by id desc limit " + PageUtil.getStartRow(currentPage, PageUtil.PAGESIZE) + "," + PageUtil.PAGESIZE + "");

        List<Distribution> result = new ArrayList<Distribution>();
        Distribution dis = null;
        List lists = this.jdbcTemplate.queryForList(sql.toString());
        Iterator it = lists.iterator();
        while (it.hasNext()) {
            Map map = (Map) it.next();
            dis = new Distribution();
            dis.setId(Integer.valueOf(String.valueOf(map.get("id"))));
            dis.setDistributionName((String) map.get("distributionName"));
            result.add(dis);
        }

        return result;
    }

    /**
     * 根据分拨点信息查询分拨点量(分页)
     *
     * @param distribution
     * @return
     */
    public int queryDistributionCount(Distribution distribution) {
        StringBuilder sql = new StringBuilder("select id from distribution where 1 = 1");
        if (0 != distribution.getId()) {
            sql.append(" and id = " + distribution.getId());
        }
        if (!StringUtils.isEmpty(distribution.getDistributionName())) {
            sql.append(" and distributionName = '" + distribution.getDistributionName() + "'");
        }

        List lists = this.jdbcTemplate.queryForList(sql.toString());
        return lists.size();
    }

    /**
     * 分拨点总页数
     *
     * @return
     */
    public int distributionTotalPageNum(int distributionCount) {
        return PageUtil.getTotalPageNum(distributionCount, PageUtil.PAGESIZE);
    }

    /**
     * 添加分拨点
     *
     * @param distribution
     * @return
     */
    public boolean addDistribution(Distribution distribution) {
        String sql = "insert into distribution(distributionName) values (?)";
        this.jdbcTemplate.update(sql, new Object[]{distribution.getDistributionName()});
        return true;
    }

    /**
     * 修改分拨点
     *
     * @param distribution
     * @return
     */
    public boolean modifyDistribution(Distribution distribution) {
        String sql = "update distribution set distributionName = ?  where id = ?";
        this.jdbcTemplate.update(sql, new Object[]{distribution.getDistributionName(), distribution.getId()});
        return true;
    }

    /**
     * 根据分拨点编号删除分拨点
     *
     * @param id
     * @return
     */
    public boolean removeDistribution(int id) {
        String sql = "delete from distribution where id = ?";
        this.jdbcTemplate.update(sql, new Object[]{id});
        return true;
    }
}